﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using DBEmr.Model;

namespace DBUtility
{
    public class DBTools
    {
        #region 变量定义
        protected string _name = string.Empty;                 // 姓名
        protected string _gender = string.Empty;                 // 性别: 男/女/未知
        //protected DateTime _birthday = DataType.DateTime_Null();     // 出生日期
        protected string _body_Height = string.Empty;                 // 身高
        protected string _body_Weight = string.Empty;                 // 体重
        #endregion

        public DICINFO getSQL(string name, ArrayList list1)
        { 
           foreach(DICINFO str1 in list1)
           {
               if (str1.Name.Equals(name))
               {
                   return str1;
               }
           }
           return null;
        }

        /// <summary>
        /// 获取某个员工信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public DBEmr.Model.EMPLOYEE getEMP(string id, ArrayList list1)
        {
            foreach (EMPLOYEE emp in list1)
            {
                if (emp.ID.Equals(id))
                {
                    return emp;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据用户姓名获取ID
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public static string getEMRIDByName(string userName, ArrayList list1)
        {

            foreach (EMPLOYEE emp in list1)
            {
                if (emp.EM_NAME.Equals(userName))
                {
                    return emp.ID;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据id获取职业名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public DBEmr.Model.METIER getMetierModel(string id, ArrayList list1)
        {
            foreach (METIER emp in list1)
            {
                if (emp.M_ID.Equals(id))
                {
                    return emp;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据id获取民族名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public DBEmr.Model.NATION getNationModel(string id, ArrayList list1)
        {
            foreach (NATION emp in list1)
            {
                if (emp.ID.Equals(id))
                {
                    return emp;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据id获取国家名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public DBEmr.Model.NATIONALITY getContryModel(string id, ArrayList list1)
        {
            foreach (NATIONALITY emp in list1)
            {
                if (emp.N_ID.Equals(id))
                {
                    return emp;
                }
            }
            return null;
        }
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public DBEmr.Model.DEPARTMENT getDEP(string id, ArrayList list1)
        {
            foreach (DEPARTMENT emp in list1)
            {
                if (emp.ID.Equals(id))
                {
                    return emp;
                }
            }
            return null;
        }


        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public string getDEPID(string name, ArrayList list1)
        {
            foreach (DEPARTMENT emp in list1)
            {
                if (emp.NAME.Equals(name))
                {
                    return emp.ID;
                }
            }
            return null;
        }


        #region 方法
        /// <summary>
        /// 获取年龄字符串
        /// </summary>
        /// <returns></returns>
        
        public static string GetAge(DateTime dtBirthday, DateTime dtNow)
        {
            string strAge = string.Empty;                         // 年龄的字符串表示
            int intYear = 0;                                    // 岁
            int intMonth = 0;                                    // 月
            int intDay = 0;                                    // 天

            

            // 计算天数
            intDay = dtNow.Day - dtBirthday.Day;
            if (intDay < 0)
            {
                dtNow = dtNow.AddMonths(-1);
                intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
            }

            // 计算月数
            intMonth = dtNow.Month - dtBirthday.Month;
            if (intMonth < 0)
            {
                intMonth += 12;
                dtNow = dtNow.AddYears(-1);
            }

            // 计算年数
            intYear = dtNow.Year - dtBirthday.Year;

            // 格式化年龄输出
            if (intYear >= 1)                                            // 年份输出
            {
                strAge = intYear.ToString() + "岁";
            }

            if (intMonth > 0 && intYear <= 5)                           // 五岁以下可以输出月数
            {
                strAge += intMonth.ToString() + "月";
            }

            if (intDay >= 0 && intYear < 1)                              // 一岁以下可以输出天数
            {
                if (strAge.Length == 0 || intDay > 0)
                {
                    strAge += intDay.ToString() + "日";
                }
            }

            return strAge;
        }

       
        #endregion
    }
}
